from django.core.management.base import BaseCommand
from django.db import transaction
from apps.product.models import Category, Product, SKU
from decimal import Decimal

class Command(BaseCommand):
    help = '初始化测试数据'

    def handle(self, *args, **options):
        self.stdout.write('开始初始化测试数据...')
        
        try:
            with transaction.atomic():
                # 清除现有数据
                Category.objects.all().delete()
                Product.objects.all().delete()
                SKU.objects.all().delete()
        
        # 创建分类
                categories = {
                    '热菜': self.create_category('热菜', 1),
                    '凉菜': self.create_category('凉菜', 2),
                    '主食': self.create_category('主食', 3),
                    '汤类': self.create_category('汤类', 4),
                    '酒水': self.create_category('酒水', 5),
                }

                # 创建热菜
                hot_dishes = [
                    {
                        'name': '宫保鸡丁',
                        'description': '经典川菜，口感麻辣鲜香',
                        'skus': [
                            {'name': '小份', 'price': Decimal('28.00'), 'stock': 100},
                            {'name': '大份', 'price': Decimal('38.00'), 'stock': 100},
                        ]
                    },
                    {
                        'name': '鱼香肉丝',
                        'description': '传统川菜，酸甜可口',
                        'skus': [
                            {'name': '小份', 'price': Decimal('26.00'), 'stock': 100},
                            {'name': '大份', 'price': Decimal('36.00'), 'stock': 100},
                        ]
                    },
                    {
                        'name': '麻婆豆腐',
                        'description': '川菜代表，麻辣鲜香',
                        'skus': [
                            {'name': '小份', 'price': Decimal('22.00'), 'stock': 100},
                            {'name': '大份', 'price': Decimal('32.00'), 'stock': 100},
                        ]
                    }
                ]

                # 创建凉菜
                cold_dishes = [
                    {
                        'name': '口水鸡',
                        'description': '开胃凉菜，麻辣鲜香',
                        'skus': [
                            {'name': '小份', 'price': Decimal('24.00'), 'stock': 100},
                            {'name': '大份', 'price': Decimal('34.00'), 'stock': 100},
                        ]
                    },
                    {
                        'name': '凉拌黄瓜',
                        'description': '清爽可口，开胃解腻',
                        'skus': [
                            {'name': '例份', 'price': Decimal('12.00'), 'stock': 100},
                        ]
                    }
                ]

                # 创建主食
                staples = [
                    {
                        'name': '米饭',
                        'description': '东北大米，香糯可口',
                        'skus': [
                            {'name': '小碗', 'price': Decimal('2.00'), 'stock': 200},
                            {'name': '大碗', 'price': Decimal('3.00'), 'stock': 200},
                        ]
                    },
                    {
                        'name': '馒头',
                        'description': '纯手工制作，松软可口',
                        'skus': [
                            {'name': '一个', 'price': Decimal('1.50'), 'stock': 200},
                            {'name': '四个', 'price': Decimal('5.00'), 'stock': 200},
                        ]
                    }
                ]

                # 创建汤类
                soups = [
                    {
                        'name': '番茄蛋汤',
                        'description': '营养美味，开胃解腻',
                        'skus': [
                            {'name': '小碗', 'price': Decimal('8.00'), 'stock': 100},
                            {'name': '大碗', 'price': Decimal('12.00'), 'stock': 100},
                        ]
                    },
                    {
                        'name': '紫菜蛋花汤',
                        'description': '清淡可口，营养丰富',
                        'skus': [
                            {'name': '小碗', 'price': Decimal('6.00'), 'stock': 100},
                            {'name': '大碗', 'price': Decimal('10.00'), 'stock': 100},
                        ]
                    }
                ]

                # 创建酒水
                drinks = [
                    {
                        'name': '可口可乐',
                        'description': '百年品牌，畅爽清凉',
                        'skus': [
                            {'name': '330ml', 'price': Decimal('3.00'), 'stock': 200},
                            {'name': '500ml', 'price': Decimal('4.00'), 'stock': 200},
                        ]
                    },
                    {
                        'name': '雪花啤酒',
                        'description': '清爽怡人，冰镇畅饮',
                        'skus': [
                            {'name': '330ml', 'price': Decimal('6.00'), 'stock': 200},
                            {'name': '500ml', 'price': Decimal('8.00'), 'stock': 200},
                        ]
                    }
                ]

                # 创建所有商品和SKU
                self.create_products(hot_dishes, categories['热菜'])
                self.create_products(cold_dishes, categories['凉菜'])
                self.create_products(staples, categories['主食'])
                self.create_products(soups, categories['汤类'])
                self.create_products(drinks, categories['酒水'])

                self.stdout.write(self.style.SUCCESS('测试数据初始化成功！'))

        except Exception as e:
            self.stdout.write(self.style.ERROR(f'初始化失败：{str(e)}'))
            raise e

    def create_category(self, name, sort_order):
        category = Category.objects.create(
            name=name,
            sort_order=sort_order,
            is_active=True
        )
        self.stdout.write(f'创建分类：{name}')
        return category

    def create_products(self, products_data, category):
        for product_data in products_data:
            product = Product.objects.create(
                category=category,
                name=product_data['name'],
                description=product_data['description'],
                is_active=True
            )
            self.stdout.write(f'创建商品：{product.name}')

            for sku_data in product_data['skus']:
                sku = SKU.objects.create(
                    product=product,
                    name=sku_data['name'],
                    price=sku_data['price'],
                    stock=sku_data['stock'],
                    is_active=True
                )
                self.stdout.write(f'创建SKU：{product.name} - {sku.name}') 